﻿using System.Collections.Generic;
using Microsoft.FxCop.Sdk;

namespace LINQtoFxCop.Core
{
  public static class TypeExtenders
  {
    internal static bool IsModuleType(this TypeNode type)
    {
      return type.Name.ToString().StartsWith("<");
    }
    public static bool IsSubTypeOf(this TypeNode type, IEnumerable<string> candidatesFullNames)
    {
      return candidatesFullNames.HasItemWhen(candidateFullName => IsSubTypeOf(type, candidateFullName));
    }

    public static bool IsSubTypeOf(this TypeNode type, string candidateTypeFullName)
    {
      if (type.IsTypeOf(candidateTypeFullName))
        return true;
      if (type.BaseType == null)
        return false;
      return type.BaseType.IsSubTypeOf(candidateTypeFullName);
    }
    private static bool IsTypeOf(this TypeNode type, string candidateTypefullName)
    {
      return type.FullName == candidateTypefullName;
    }
    public static string BaseTypeNames(this TypeNode type)
    {
      //We don't care about System.Object, since it is implicitly given
      if (type.BaseType.BaseType == null)
        return string.Empty;

      var baseTypeNames = BaseTypeNames(type.BaseType);
      if (baseTypeNames == string.Empty)
        return type.BaseType.Name.Name;

      return type.BaseType.Name.Name + "." + baseTypeNames;
    }
  }
}
